package com.jubotech.framework.netty.handler.socket;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.google.protobuf.util.JsonFormat;
import com.jubotech.business.web.domain.CommonState;
import com.jubotech.business.web.domain.WxAccountInfo;
import com.jubotech.business.web.service.CommonStateService;
import com.jubotech.business.web.service.WxAccountService;
import com.jubotech.framework.common.AutoType;
import com.jubotech.framework.netty.async.AsyncTaskService;
import com.jubotech.framework.netty.common.Constant;
import com.jubotech.framework.netty.handler.MessageHandler;
import com.jubotech.framework.netty.utils.MessageUtil;
import com.jubotech.framework.util.MessageSendUtils;

import Jubo.JuLiao.IM.Wx.Proto.FriendAddReqeustNotice.FriendAddReqeustNoticeMessage;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.EnumErrorCode;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.EnumMsgType;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.TransportMessage;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class FriendAddReqeustNoticeHandler implements MessageHandler{
	@Autowired
	private WxAccountService weChatAccountService;
	@Autowired
	private AsyncTaskService asyncTaskService;
	@Autowired
	private CommonStateService commonStateService;

	/**
	 * 个人号好友请求通知---手机端经过服务端转发给pc端
	 * 
	 * @author wechatno:tangjinjinwx
     * @blog http://www.wlkankan.cn
	 */
	@Async
	public void handleMsg(ChannelHandlerContext ctx, TransportMessage vo) {
		try {
			FriendAddReqeustNoticeMessage req = vo.getContent().unpack(FriendAddReqeustNoticeMessage.class);
			log.debug(JsonFormat.printer().print(req));
			WxAccountInfo account = weChatAccountService.findWeChatAccountInfoByWeChatId(req.getWeChatId());
			if (null != account) {
				// 自动通过好友拦截器
				asyncTaskService.msgAopTaskFriendAdd(vo, ctx, req);

				// 查看是否配置自动通过后的回复语,即第一次给好友发消息
				firstToFirendMsg(ctx, req, account);

				// 转发消息到PC
				asyncTaskService.msgSend2pc(req.getWeChatId(), EnumMsgType.FriendAddReqeustNotice, req);
			} else {// 如果pc端没在线，先帮忙自动通过了再说
				MessageSendUtils.throughFriendRequest(vo, req, ctx);
			}
			// 告诉客户端消息已收到
			MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
		} catch (Exception e) {
			MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam, vo.getId(), Constant.ERROR_MSG_DECODFAIL);
			e.printStackTrace();
		}
	}

	/**
	 * 给好友发消息
	 * 
	 * @param ctx
	 * @param req
	 * @param account
	 */
	private void firstToFirendMsg(ChannelHandlerContext ctx, FriendAddReqeustNoticeMessage req,
			WxAccountInfo account) {
		// 查看是否配置自动通过后的回复语
		CommonState commonState = commonStateService.queryCommonStateByCid(account.getCid(),AutoType.AUTOTYPE_BYREQUESTAFTER);
		if (null != commonState && !StringUtils.isBlank(commonState.getReturnString())  && commonState.getState()==0) {
			MessageSendUtils.sendMessage(ctx, req.getFriendId(), commonState.getReturnString());
		}
	}
}